package com.my.socket.bio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.Socket;
import java.util.concurrent.Callable;

/**
 * User: Alex Zhong
 * Date: 2016/6/17
 */
public class HandleCallable implements Callable<Void> {
    private static final Logger LOGGER = LoggerFactory.getLogger(HandleCallable.class);

    private Socket socket;

    public HandleCallable(Socket socket) {
        assert socket != null;
        this.socket = socket;
    }

    @Override
    public Void call() throws Exception {
        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String message = bufferedReader.readLine();

        LOGGER.info("receive the message [{}] from the client {}", message, socket.getPort());
        message = message.toUpperCase();

        OutputStream outputStream = socket.getOutputStream();
        PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(outputStream));
        printWriter.println(message);
        printWriter.flush();
        LOGGER.info("send the message [{}] to the client {}", message, socket.getPort());

        return null;
    }
}
